package com.wqh.user.controller;

import com.wqh.api.dto.User;
import com.wqh.api.service.UserService;
import com.wqh.user.repository.UserRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

/**
 * @author wanqh
 * @date 2018/01/16 11:25
 * @description:
 */
@RestController
public class UserRafactorController implements UserService{
    private final Logger logger = LoggerFactory.getLogger(UserRafactorController.class);
    @Autowired
    private UserRepository userRepository;
    @Override
    public List<User> findAll() {
        return null;
    }

    @Override
    public User finUserById(Long id) {
        logger.info(">>>>>>>>>>>Rafactor id:{}<<<<<<<<<<<<<",id);
        com.wqh.user.entity.User one = userRepository.findOne(id);
        User user = new User(one.getId(),one.getUsername(),one.getAge());
        return user;
    }

    @Override
    public User findUserHeader(@RequestHeader("name")String name, @RequestHeader("id")Long id,@RequestHeader("age") Integer age) {
        User user = new User();
        user.setId(id);
        try {
            user.setUsername( URLDecoder.decode(name,"UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        user.setAge(age);
        logger.info(">>>>>>>>>>>Rafactor findUserHeader{}<<<<<<<<<<<<<",user);
        return user;
    }

    @Override
    public User insertUser(@RequestBody User user) {
        logger.info(">>>>>>>>>>>Rafactor RequestBody{}<<<<<<<<<<<<<",user);
        return user;
    }
}
